// ********************************************
//         APM unitary tests software v1.0.x
//			for APM with PIC 18F26K20
//
//	Author : Gilles KREBS
//
//  No commercial use without a written permission of the author

//
// Includes
#include "GENp18.h"
#include "IOSp18.h"
#include "SYSp18.h"
#include "TTYp18.h"
#include "main.h"

// Prototypes
VOID APM_hal_init(VOID);

// Defines
#define OUT 0
#define IN 1

// Configuration bits
#pragma romdata CONFIG1H = 0x300001
const rom unsigned char config1H = 0b00001000;      // HS clock

#pragma romdata CONFIG2L = 0x300002
const rom unsigned char config2L = 0b00011110;      // Brown-out Reset Enabled in hardware @ 2.0V, PWRTEN enabled

#pragma romdata CONFIG2H = 0x300003
const rom unsigned char config2H = 0b00010000; //0b00001010;      // HW WD disabled, 1:32 prescaler

#pragma romdata CONFIG3H = 0x300005
const rom unsigned char config3H = 0b10000000;      // PORTB digital on RESET

#pragma romdata CONFIG4L = 0x300006
const rom unsigned char config4L = 0b11000001;      // DEBUG disabled,
	                                                // XINST enabled
	                                                // LVP disabled
	                                                // STVREN enabled


                            
VOID main(VOID)
{
	OSCCON = 0x72;
	
	while(OSCCONbits.IOFS == 0);
	
	// System init
	SYSopen(16000000);
	
	// Init HAL
	APM_hal_init();
	
	// Open and configure USART
	TTYopen(0,
			TTY_ASYNCH_MODE &
			TTY_EIGHT_BIT &
			TTY_CONT_RX &
			TTY_BRGH_HIGH,
			(DWORD)9600 );
	
	TTYprintf(0, (ROM CHAR *)"\r\nHELLO WORLD!");

	while(1)
	{
	}
}

VOID APM_hal_init(VOID)
{
	//disable WDT
	WDTCONbits.SWDTEN = 0;

	// Port A is digital
	ADCON1 = 0xFF;

	// Pull ups on port B
	INTCON2 = 0x00;
	INTCON3 = 0x00;

	// RA0 - Voltage measurement analog input
	TRISAbits.TRISA0 = IN;

	// RA1 - Function PB input
	TRISAbits.TRISA1 = IN;

	// RA2 - Run LED - 1 to light
	PORTAbits.RA2 = 0;
	TRISAbits.TRISA2 = OUT;

	// RA3 to RA5 - Not used ==> outputs
	TRISAbits.TRISA3 = OUT;
	TRISAbits.TRISA4 = OUT;
	TRISAbits.TRISA5 = OUT;

	// RA6 - RA7 : Crystal oscillator - no initialization

	// RB0 - RF interrupt - configure as input
	TRISBbits.TRISB0 = IN;

	// RB1 - I2C interrupt - configure as input
	TRISBbits.TRISB1 = IN;

	// RB2 - NC ==> output
	TRISBbits.TRISB2 = OUT;

	// RB3 - NC ==> output
	TRISBbits.TRISB3 = OUT;

	// RB4 - NC ==> output
	TRISBbits.TRISB4 = OUT;

	// RB5 - RF reset - configure as output
	PORTBbits.RB5 = 1;
	TRISBbits.TRISB5 = 0;

	// RB6 - PGC
	// RB7 - PGD

	// RC0 - ADC CS - output
	PORTCbits.RC0 = 1;
	TRISCbits.TRISC0 = OUT;

	// RC1 - RF CS - output
	PORTCbits.RC1 = 1;
	TRISCbits.TRISC1 = 0;

	// RC2 - RF WAKE - output
	PORTCbits.RC2 = 1;
	TRISCbits.TRISC2 = 0;

	// RC3 - SPI SCK - has to be set as an output - Done in SPIopen()
	// RC4 - Used as SPI SDI has to be set as an input - Done in SPIopen()
	// RC5 -  SPI SDO connectedt - Set as digital output - Done in SPIopen()
	// RC6 - EUSART TXD
	// RC7 - EUSART RXD

} // End of APM_hal_init()
